// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import EventManager from "../EventManager";
import GameConfig, { COLOR, DIRECTION, ShapeData, ShapeType } from "../GameConfig";
import Utlis from "../Utlis";



const {ccclass, property} = cc._decorator;

@ccclass
export default class Shape {

    private shapeData:ShapeData;
    private interval:number = 0;
    private angle:number = 0;
    private isActive:boolean = false;

    public init(shapeData:ShapeData) {
        this.shapeData = shapeData;
        this.interval = 0;
        this.angle = 90;
        this.isActive = true;
        Utlis.writeToBigGrid(shapeData,this.angle);
    }

    public update(dt) {
        if (!this.isActive) {
            return;
        }
        this.interval += dt*GameConfig.SPEED;
        if (this.interval >= 1) {
            //向下移动
            this.down();
            this.interval = 0;
        }
    }

    

    private clearShape() {
        const color = this.shapeData.color;
        this.shapeData.color = COLOR.Null;
        Utlis.writeToBigGrid(this.shapeData, this.angle);
        this.shapeData.color = color;
    }
    public down() {
        if (!this.isActive) {
            return;
        }
        this.clearShape();
        this.shapeData.gridPos.y -= 1;


        if (!Utlis.canBlockPass(this.shapeData,this.angle)) {
            this.shapeData.gridPos.y += 1;
            this.isActive = false;
            EventManager.getInstance().emit(GameConfig.EVENT_NAME.SHAPE_FALL_TO_BOTTOM, this);
            console.error("要超出底部了");
        }
        Utlis.writeToBigGrid(this.shapeData, this.angle);

    }
    public up() {
        if (!this.isActive) {
            return;
        }
        this.clearShape();
        this.shapeData.gridPos.y += 1;
        if (Utlis.canBlockPass(this.shapeData, this.angle, DIRECTION.Up)) {
            this.shapeData.gridPos.y -= 1;
            console.error("要超出顶部了");
        }
        Utlis.writeToBigGrid(this.shapeData, this.angle);
    }

    public left() {
        if (!this.isActive) {
            return;
        }
        this.clearShape();
        this.shapeData.gridPos.x -= 1;
        if (!Utlis.canBlockPass(this.shapeData, this.angle, DIRECTION.Left)) {
            this.shapeData.gridPos.x += 1;
            console.error("要超出左边了");
        }
        Utlis.writeToBigGrid(this.shapeData, this.angle);
    }

    public right() {
        if (!this.isActive) {
            return;
        }
        this.clearShape();
        this.shapeData.gridPos.x += 1;
        if(!Utlis.canBlockPass(this.shapeData, this.angle, DIRECTION.Right)) {
            console.error("要超出右边了");
            this.shapeData.gridPos.x -= 1;
        }
        Utlis.writeToBigGrid(this.shapeData, this.angle);
    }

    public destroy() {
        this.clearShape();
        this.shapeData = null;
    }

    
}
